package dao;

import bean.Loginbean;

import java.sql.*;

public class LoginDao {
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String USERNAME = "root";
    private static final String PWD = "123456";
    /**
     * 模型层：用于登录
     */

    public static int login(Loginbean loginbean){
        int result = -1;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
            String sql = "select count(*) from user where username=? and password=?";
            pstmt =  connection.prepareStatement(sql);
            pstmt.setString(1,loginbean.getName());
            pstmt.setString(2,loginbean.getPassword());
            rs = pstmt.executeQuery();
            while (rs.next()){
                result = rs.getInt(1);
            }
            if (result >0){ //成功
                return 1;
            }
            else {
                return 0; //失败
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return -1; //异常

        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
        finally {
            try {
                if (rs!=null){
                    rs.close();
                }
                if (pstmt!=null){
                    pstmt.close();
                }
                if (connection!=null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return -1;
            }

        }

    }

    /**
     * 查询数据库里面是否有相同的值
     * 1.有返回false
     * 2.没有返回true
     * @param loginbean
     * @return
     */
    public boolean YanZ(Loginbean loginbean){
        PreparedStatement pstmt = null;
        Connection connection = null;
        ResultSet rs = null;
        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL,USERNAME,PWD);
            String sql ="select count(*) from user where username=?";
            pstmt =  connection.prepareStatement(sql);
            pstmt.setString(1,loginbean.getName());
            rs = pstmt.executeQuery();
            while (rs.next()){
                result = rs.getInt(1);
            }
            if (result>0){//存在
                return false;
            }
            else{//0:不存在
                return true;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (rs!=null){
                    rs.close();
                }
                if (pstmt!=null){
                    pstmt.close();
                }
                if (connection!=null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return true;
    }

    /**
     * 添加
     * @param loginbean
     */
    public void Register(Loginbean loginbean){
        PreparedStatement pstmt = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL,USERNAME,PWD);

            String sql = "insert into user(username,password) values (?,?)";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1,loginbean.getName());
            pstmt.setString(2,loginbean.getPassword());
            pstmt.executeUpdate();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if (pstmt!=null){
                    pstmt.close();
                }
                if (connection!=null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}
